// InterpolationTest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "stdio.h"
#include "math.h"
#include "string.h"
#include "PatternInterpolation.h"
#include <fstream>
#include "Common.h"

using namespace std;



static inline int iClip3(int low, int high, int x);
void InterpolateMB(imgpel **full_rec, imgpel ** arr, int width, int height, int mb_x, int mb_y, int img_width, int img_height );



double DCTIKernel[17][9];
double dct_s[9] = { 1/2, 1, 1, 1, 1, 1, 1, 1, 1/2 };

//#define N 8

imgpel orig_pic[352][288];

double KD( int a, int b )
{
	if ( a == b ) return 1;
	else return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{

	// init_DCTIKenel();
	// Test interpolation by DCT-I
	//int num = 9;
	//double A[] = { 10, 40, 201, 255, 24, 254, 254, 254, 254 };
	//double B[9];
	//double C[17];
	//double D[17];
	//memset( C, 0, 17 * sizeof( double ) );

	//printf( "A-> " ); for ( int i=0; i<num; i++ ) { printf( "%05.5f ", A[i] ); } printf( "\n" ); printf( "\n" );

	//ComputeDCTI( A, B, num );
	//printf( "B-> " ); for ( int i=0; i<num; i++ ) { printf( "%05.5f ", B[i] ); } printf( "\n" ); printf( "\n" );
	//
	//ComputeIDCTI( B, A, num );
	//printf( "A-> " ); for ( int i=0; i<num; i++ ) { printf( "%05.5f ", A[i]); } printf( "\n" ); printf( "\n" );

	//memcpy( C, B, 9 * sizeof( double ) );
	//C[8] = C[8]/2;	// important!
	//printf( "C-> " ); for ( int i=0; i<17; i++ ) { printf( "%05.5f ", C[i] ); } printf( "\n" ); printf( "\n" );

	//ComputeIDCTI( C, D, 17 );
	//printf( "D-> " ); for ( int i=0; i<17; i++ ) { printf( "%05.5f ", D[i]); } printf( "\n" ); printf( "\n" );


	//return 0;

	FILE * orig_pic_file = fopen( "foreman.frame1", "r" );

	if ( orig_pic_file == NULL ) return -1;

	// imgpel inter_pic[352][288];
	//imgpel (*inter_pic)[352] = new imgpel[288][352];
	
	imgpel ** inter_pic = new imgpel*[288];
	for( int i=0; i<288; i++ )
	{
		inter_pic[i] = new imgpel[352];
	}

	unsigned char buff[352*288];

	size_t read_size = fread( buff, 1, 352*288, orig_pic_file );

	if ( read_size != 352*288 )
	{
		return -1;
	}


	for ( int j=0; j<288; j++ )
	{
		for( int i=0; i<352; i++ )
		{
			orig_pic[i][j] = buff[j*352+i];
			// printf( "%d, %d", orig_pic[i][j], buff[i*352+j] );
			if ( i%2 == 0 )
			{
				inter_pic[j][i] = buff[j*352+i];
			}
		}
	}


	for ( int mb_y = 0; mb_y<288/16; mb_y++ )
	{
		for ( int mb_x=0; mb_x<352/16; mb_x++ )
		{
			for ( int y=0; y<16; y++ )
			{
				for ( int x=1; x<16; x+=2 )
				{
					inter_pic[mb_y*16+y][mb_x*16+x] = InterpolatePixelWithPattern( inter_pic, 16, 16, x, y, mb_x, mb_y );
				}
			}
		}
	}


	fstream infile;
	infile.open( "interdump.yuv", ios::binary|ios::out );
	if ( !infile.is_open() )
	{
		return false;
	}

	unsigned char tempbuff[352];
	for ( int y=0; y<288; y++ )
	{
		for ( int x=0; x<352; x++ )
		{
			tempbuff[x] = (unsigned char) inter_pic[y][x];
		}
		infile.write( (char*) tempbuff, 352 );
	}

	infile.close();

	return 0;
}


